<template>
  <div>
    <van-nav-bar title="黑马头条-登录" :fixed="true" placeholder/>
    <van-form @submit="onSubmit">
      <van-field
        v-model="form.mobile"
        name="mobile"
        label="手机号"
        placeholder="手机号"
        required
        :rules="[{ required: true, message: '请填写手机号' ,pattern: /^1[3-9]\d{9}$/ }]"
      />
      <van-field
        v-model="form.code"
        name="code"
        type="password"
        label="验证码"
        placeholder="验证码"
        required
        :rules="[{ required: true, message: '请填写验证码' ,pattern: /^\d{6}$/ }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" :disabled ="isLogin" :loading="isLogin" loading-text="玩儿命登录中..."
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { setToken } from '@/utils/token'
import { Notify } from 'vant'
export default {
  data () {
    return {
      form: {
        mobile: '',
        code: '246810'
      },
      isLogin: false

    }
  },
  methods: {
    async  onSubmit (values) {
      this.isLogin = true
      //  console.log('submit', values)
      try {
        const res = await loginAPI({
          ...this.form
        })
        setToken(res.data.data.token)
        Notify({ type: 'success', message: '恭喜你!登录成功!' })
        this.isLogin = false
        this.$router.push('/layout/home')
        //   console.log(res)
      } catch (err) {
        Notify({ type: 'danger', message: '手机号或验证码错误!登录失败!' })
        this.isLogin = false
      }
    }
  }
}
</script>

<style></style>
